package com.atguigu.jdkOnePointEight;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Guo 2022/11/18 17:54:36
 */
public class GetDiffDate {

    public static void main(String[] args) {

        String st = "2011-08-19";// 开始时间
        String et = "2017-06-14";// 结束时间
        try {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            Date d1 = df.parse(st);
            Date d2 = df.parse(et);

            int sy = Integer.valueOf(st.split("-")[0]);
            int sm = Integer.valueOf(st.split("-")[1]);

            int ey = Integer.valueOf(et.split("-")[0]);
            int em = Integer.valueOf(et.split("-")[1]);

            long diff = d2.getTime()-d1.getTime();

            int runtian = 0;
            //判断有多少个闰月在区间内
            int num_runyue = 0;
            for (int i = sy; i < ey + 1; i++) {
                if (i % 4 == 0) {
                    num_runyue++;
                    runtian++;
                }
            }
            // 闰月不在起始年的月范围
            if (sm >=2 && sy % 4 == 0) {
                num_runyue--;
            }
            // 闰月不在结束始年的月范围
            if (em <= 2 && ey % 4 == 0) {
                num_runyue--;
            }

            long days = diff / (1000 * 60 * 60 * 24);
            // 把所有年都整理成365天
            days = days - num_runyue;

            System.out.println("相差"+days/365+"年"+(days%365)+"天");
        } catch (Exception e) {
            System.out.println("异常报错");
        }
    }
}
